<?php

	// TITLE: Payment AUTO MODEL
	// FILE: scaffolding/model/payment.php
	// AUTHOR: AUTOGEN
	// CREATED: 2015/04/20 07:49:57
	// DESCRIPTION: 
	// REQUIREMENTS: 

	$createPayment = function($con, $payment) {
		$sql = 'INSERT INTO payment (id, mortgage_id, amount, date, person_id) VALUES (';
		$sql .= $payment->getId() . ', ';
		$sql .= $payment->getMortgageId() . ', ';
		$sql .= $payment->getAmount() . ', ';
		$sql .= $payment->getDate() . ', ';
		$sql .= $payment->getPersonId() . ')';
		$sql .= ')';
		$result = mysqli_query($con, $sql);
		$id = ($result) ? mysqli_insert_id($con) : $result;
		mysqli_close($con);
		return $id;
	};

	$readPayment = function($con, $id) {
		$sql = 'SELECT * FROM payment WHERE id = ' . $id;
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$result = mysqli_fetch_array($results);
			$payment = new Payment();
			$payment->setId(result[0]);
			$payment->setMortgageId($dbio->select($readMortgage(result[1])));
			$payment->setAmount(result[2]);
			$payment->setDate(result[3]);
			$payment->setPersonId($dbio->select($readPerson(result[4])));
		} else {
			$payment = false;
		}
		return $payment;
	};

	$updatePayment = function($con, $payment) {
		$sql = 'UPDATE payment SET ';
		$sql .= 'id = ' . $payment->getId() . ', ';
		$sql .= 'mortgage_id = ' . $payment->getMortgageId() . ', ';
		$sql .= 'amount = ' . $payment->getAmount() . ', ';
		$sql .= 'date = ' . $payment->getDate() . ', ';
		$sql .= 'person_id = ' . $payment->getPersonId() ;
		$sql .= 'WHERE id = ' . id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$deletePayment = function($con, $id) {
		$sql = 'DELETE FROM payment WHERE id = ' . $id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$listPayment = function($con) {
		$sql = 'SELECT * FROM payment';
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$payments = array();
			while ($result = mysqli_fetch_array($results)) {
				$payment = new Payment();
				$payment->setId(result[0]);
				$payment->setMortgageId($dbio->select($readMortgage(result[1])));
				$payment->setAmount(result[2]);
				$payment->setDate(result[3]);
				$payment->setPersonId($dbio->select($readPerson(result[4])));
				$payments[] = payment;
			}// end while
		} else {
			$payments = false;
		}
		return $payments;
	};

